package client.ui.view.dialogview;

import base.BaseValue;
import client.assets.Icons;
import client.connect.HttpConnect;
import client.control.Controller;
import client.control.ResponseEntity;
import client.ui.component.button.MaterialButton;
import client.ui.component.edittext.PasswordEdit;
import client.ui.component.edittext.TextEdit;
import com.alibaba.fastjson.JSONObject;
import util.MD5Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

/**
  * @Description 注册对话框
  * @author 孔明浩
  * @Date 2020/3/21
  */
public class RegisterDialog {
    /**
     * @Description
     * @param owner 对话框所属
     * @param parentContainer 对话框父组件
     * @author 孔明浩
     * @Date 2020/3/31
     */
    public static void showRegisterDialog(Frame owner, Container parentContainer){
        JDialog dialog = new JDialog(owner,true);
        dialog.setSize(350,280);
        dialog.setResizable(false);
        dialog.setLocationRelativeTo(parentContainer);
        dialog.setTitle("注册账户");
        dialog.setIconImage(Icons.register.getImage());

        JButton confirm = MaterialButton.BlueButton("确认注册",null);
        TextEdit usernameEdit = new TextEdit("请输入用户名");
        PasswordEdit passwordEdit = new PasswordEdit("请输入注册密码");
        PasswordEdit confirmPassword = new PasswordEdit("请确认密码");

        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (usernameEdit.getText().length() == 0){
                    usernameEdit.setErrorText("用户名不能为空");
                    passwordEdit.setErrorText("");
                    confirmPassword.setErrorText("");
                } else if (passwordEdit.getText().length() == 0){
                    passwordEdit.setErrorText("密码不能为空");
                    usernameEdit.setErrorText("");
                    confirmPassword.setErrorText("");
                } else if (!confirmPassword.getText().equals(passwordEdit.getText())){
                    confirmPassword.setErrorText("两次输入密码不一致");
                    usernameEdit.setErrorText("");
                    passwordEdit.setErrorText("");
                } else {
                    //启动新线程查询数据
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            ResponseEntity responseEntity = Controller.USER_CONTROL.register(
                                    usernameEdit.getText(),MD5Util.encodeByMd5(passwordEdit.getText())
                            );
                            if (responseEntity.getCode() == 0){
                                //关闭对话框
                                SwingUtilities.invokeLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        dialog.dispose();
                                        JOptionPane.showMessageDialog(owner,"注册成功");
                                    }
                                });
                            } else if (responseEntity.getCode() == 1){
                                usernameEdit.setErrorText("用户名已经存在");
                            } else {
                                JOptionPane.showMessageDialog(owner,"注册失败");
                            }
                        }
                    }).start();
                }
            }
        });

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        Box vBox = Box.createVerticalBox();
        vBox.add(usernameEdit);
        vBox.add(passwordEdit);
        vBox.add(confirmPassword);
        vBox.add(new JLabel(" "));
        panel.add(vBox);
        panel.add(confirm);
        dialog.setContentPane(panel);

        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setUndecorated(false);
        dialog.setVisible(true);
    }

}
